<template>
  <div class="app-container">
    <el-card>
      <!-- 標籤頁 -->
      <!--  @tab-click="handleClick" -->
      <el-tabs v-model="activeName">
        <el-tab-pane label="用户管理" name="first">
          <!-- 表單項 -->
          <el-form ref="userInfoRef" :model="userInfo" :rules="updInfoRules" label-width="120px" style="margin-left: 120px; margin-top:30px">
            <el-form-item label="姓名">
              <el-input v-model="userInfo.username" style="width:300px" />
            </el-form-item>
            <el-form-item label="密码" prop="password2">
              <el-input v-model="userInfo.password2" type="password" style="width:300px" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="updUserInfo">更新</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="个人详情" name="second">
          <!-- 打印按钮 -->
          <el-row type="flex" justify="end">
            <el-tooltip content="打印个人基本信息">
              <router-link :to="`/employees/print/${userId}?type=personal`">
                <i class="el-icon-printer" />
              </router-link>
            </el-tooltip>
          </el-row>
          <!-- 放置个人详情 -->
          <component :is="userInfoCom" />
        </el-tab-pane>
        <el-tab-pane label="角色管理" name="third">
          <!-- 打印按钮 -->
          <el-row type="flex" justify="end">
            <el-tooltip content="打印岗位信息">
              <router-link :to="`/employees/print/${userId}?type=job`">
                <i class="el-icon-printer" />
              </router-link>
            </el-tooltip>
          </el-row>
          <component :is="jobInfo" />
        </el-tab-pane>
      </el-tabs>
    </el-card>
  </div>
</template>

<script>
import { getUserDetailById, updUserDetailById } from '@/api/user'
import UserInfo from './user-info'
import jobInfo from '@/views/employees/components/job-info'

export default {
  name: 'DetailsPage',
  components: {
    UserInfo,
    jobInfo
  },
  data () {
    return {
      userInfoCom: 'UserInfo',
      jobInfo,
      userId: this.$route.params.id, // 直接从路径获取用户 ID
      activeName: 'first',
      userInfo: {
        username: '',
        password2: ''
      },
      updInfoRules: {
        username: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        password2: [{ required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 12, message: '密码的长度为6-12位', trigger: 'blur' }]
      }
    }
  },
  created () {
    this.getUserInfo()
  },
  methods: {
    // 获取用户信息
    // TODO: 问题：很多接口出现重复调用  不能调用一次直接传数据？？
    async getUserInfo () {
      this.userInfo = await getUserDetailById(this.userId)
      // console.log(this.userInfo)
    },
    // 更新用户信息
    async updUserInfo () {
      try {
        // 手动调用校验
        this.$refs.userInfoRef.validate()
        // 将新密码覆盖原来的密码
        const res = await updUserDetailById({ ...this.userInfo, password: this.userInfo.password2 })
        console.log(res)
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style>

</style>
